﻿namespace dotnet_notes.Tools
{
    /// <summary>
    /// 文件工具类
    /// </summary>
    public static class FileTool
    {
        /// <summary>
        /// 获取 wwwroot 文件夹路径
        /// </summary>
        public static string GetWwwrootPath()
        {
            return Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
        }

        /// <summary>
        /// 检查文件后缀是否有效
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static bool CheckSuffix(string fileName)
        {
            string suffix = Path.GetExtension(fileName);
            return FileConfig.FileSuffixArray.Contains(suffix);
        }

        /// <summary>
        /// 通过文件后缀和当前时间获取文件名
        /// </summary>
        /// <param name="suffix">文件后缀</param>
        /// <returns></returns>
        public static string GetFileNameFromTime(string suffix)
        {
            return DateTime.Now.ToString(DateTimeConfig.DateTimeOfFile) + suffix;
        }

        /// <summary>
        /// 使用正斜杠拼接文件相对路径
        /// </summary>
        /// <param name="parts">路径部分数组</param>
        /// <returns></returns>
        public static string SpliceFilePath(params string[] parts)
        {
            var sb = new StringBuilder();
            foreach (var item in parts)
            {
                sb.Append("/" + item);
            }

            return sb.ToString();
        }

        /// <summary>
        /// 获取文件夹下的所有文件
        /// </summary>
        /// <param name="path">文件夹路径</param>
        /// <returns>文件完整路径集合</returns>
        public static List<string> GetAllFiles(string path)
        {
            var list = new List<string>();

            var files = Directory.GetFiles(path);
            list.AddRange(files);

            var directories = Directory.GetDirectories(path);
            foreach (var subDirectory in directories)
            {
                list.AddRange(GetAllFiles(subDirectory));
            }

            return list;
        }
    }
}
